#include "GameState.h"

GameState::GameState()
{
    //ctor
}

GameState::~GameState()
{
    levelStates.clear();
}

GameLevelState * GameState::GetGameLevelState(string chapter, string level)
{
    return levelStates[make_pair(chapter, level)];
}

void GameState::SetGameLevelState(string chapter, string level, GameLevelState *state)
{
    levelStates[make_pair(chapter, level)] = state;
}


void GameState::SetCurrentState(string chapter, string level, string state)
{
    GameLevelState * ls= GetGameLevelState(chapter, level);
    if (ls == NULL) {
        ls = new GameLevelState("", chapter, level);
        SetGameLevelState(chapter, level, ls);
    }

    // Don't allow downgrade from Completed status
    if (ls->GetState() != "Completed"){
        ls->SetState(state);
    }
}

string GameState::GetCurrentState(string chapter, string level)
{
    GameLevelState * ls= GetGameLevelState(chapter, level);
    if (ls == NULL) {
        return "Unavailable";
    } else {
        string result = ls->GetState();
        if (result == "") {
            return "Unavailable";
        } else {
            return result;
        }
    }
}

void GameState::LoadGameState(string filename)
{
    this->filename = filename;
}

void GameState::SaveGameState(string filename)
{
    if (filename == "") {filename = this->filename;}
    this->filename = filename;
    if (filename == "") return;
    map< pair< string,string >, GameLevelState * >::iterator I;
    for (I=levelStates.begin(); I != levelStates.end(); ++I) {

    }
}
